package ccg;

import utils.FileUtil;
import utils.GetFileEncodingByPath;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class xx {
    public static void main(String[] args) throws IOException {
        FileInputStream in = null;
        List<String> fileData = new ArrayList<String>();
        File file = new File("D:\\测试临时文件\\黑马TXT\\GBK_TXT\\622_经济经纬");
        File file11 = new File("D:\\测试临时文件\\2019年报TXT_3_乱码.txt");
        File[] files = file.listFiles();
        for (File file1 : files) {
            Long filelength = file1.length();
            byte[] filecontent = new byte[filelength.intValue()];
            try {
                in = new FileInputStream(file1);
                in.read(filecontent);
                String s = new String(filecontent, "GBK");
                if (s.contains("爛爛")) {
                    System.out.println(file1.getName());
                    fileData.add(file1.getName());
                    in.close();
                    file1.delete();
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                in.close();
            }
        }
        writerFile(file11.getPath(), fileData);
    }

    /**
     * @Author: 程存淦
     * @Description: 写文件信息
     * @DateTime: 2021年6月28日11:54:14
     * @Params:
     * @Return
     */
    public static void writerFile(String filePath,List<String> data) {
        try {

            File file = new File(filePath);
            if (file.exists())
                file.delete();
            file.createNewFile();
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "GBK"));
            data.forEach(d -> {
                try {
                    bw.write(d);
                    bw.newLine();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            });
            bw.flush();
            bw.close();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}
